/*
 * This file is part of aKoustiK.

 * aKoustiK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * aKoustiK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with aKoustiK.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <KActionCollection>
#include <KToolBar>

#include <QtCore/QAbstractTableModel>
#include <QtGui/QHeaderView>
#include <QtGui/QKeyEvent>
#include <QtGui/QStyledItemDelegate>
#include <QtGui/QTreeView>
#include <QtGui/QVBoxLayout>

#include <kore.h>
#include <ui/player/playerwidget.h>
#include <ui/player/playlistitemdelegate.h>
#include <ui/player/playlisttablemodel.h>
#include <ui/player/playlistview.h>
#include <ui/player/volumewidget.h>
#include <ui/player/seekwidget.h>
#include <ui/player/shuffleaction.h>
#include <ui/player/repeataction.h>

PlayerWidget::PlayerWidget(QWidget *parent) : QWidget(parent) {
    qDebug("PlayerWidget: constructor invoked");
    PlaylistTableModel* tableModel = new PlaylistTableModel(this);

    PlaylistView* playlistView = new PlaylistView(this);
    playlistView->setModel(tableModel);
    playlistView->setAcceptDrops(true);
    playlistView->setAlternatingRowColors(true);
    playlistView->setDragDropMode(QAbstractItemView::DragDrop);
    playlistView->setDropIndicatorShown(true);
    playlistView->setDragEnabled(true);
    playlistView->setRootIsDecorated(false);
    playlistView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    playlistView->setSelectionBehavior(QAbstractItemView::SelectRows);
    playlistView->setItemDelegate(new PlaylistItemDelegate(this));

    connect(playlistView, SIGNAL(deleteItems(QModelIndexList)), tableModel, SLOT(deleteItems(QModelIndexList)));

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(playlistView);

    {
        KToolBar* playerControls = new KToolBar(this, false, false);
        KActionCollection* actionCollection = Kore::instance().actionCollection();
        playerControls->addAction(actionCollection->action("previous"));
        playerControls->addAction(actionCollection->action("play"));
        playerControls->addAction(actionCollection->action("pause"));
        playerControls->addAction(actionCollection->action("stop"));
        playerControls->addAction(actionCollection->action("next"));
        playerControls->addSeparator();
        playerControls->addAction(new ShuffleAction(this));
        playerControls->addAction(new RepeatAction(this));

        QHBoxLayout* controlsLayout = new QHBoxLayout();
        controlsLayout->addWidget(playerControls);
        controlsLayout->addStretch();
        controlsLayout->addWidget(new VolumeWidget(this));

        layout->addLayout(controlsLayout);
    }

    layout->addWidget(new SeekWidget(this));
}

PlayerWidget::~PlayerWidget() {
    qDebug("PlayerWidget: destructor invoked");
}
